package com.example.easypoi.controller;

import com.example.easypoi.config.CommonResult;
import com.example.easypoi.model.Bank;
import com.example.easypoi.model.BankCode;
import com.example.easypoi.model.District;
import com.example.easypoi.model.Maintain;
import com.example.easypoi.service.BankCodeService;
import com.example.easypoi.service.BankService;
import com.example.easypoi.service.DistrictService;
import com.example.easypoi.util.BankCodePatientExcelUtil;
import com.example.easypoi.util.BankPatientExcelUtil;
import com.example.easypoi.util.scriptDemo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/bank")
@Api(tags = "银行卡相关")
public class BankController {


    @Resource
    private BankService bankService;

    @Resource
    private BankCodeService bankCodeService;

    @ApiOperation("银行支行编码")
    @PostMapping("/addMaintainSSS")
    public CommonResult addMaintainSSS(@RequestParam(value = "file", required = false) MultipartFile file) {
        long startTime = System.currentTimeMillis();
        List<String> list = new ArrayList();
        Map<String, Object> res = new HashMap<>();
        int row = 1;
        int rowSuccess = 0;
        Integer errorCount = 0;
        List<Bank> excelInfo = BankPatientExcelUtil.getExcelInfo(file);
        List<Bank> excelInfoList = new ArrayList<>();
        excelInfo.stream().forEach(exce -> {
            Bank bank = new Bank();
            BeanUtils.copyProperties(exce, bank);
            //判断是否包含该字
//            boolean contains = exce.getAddress().contains(name);
//            if (contains) {
//                maintainDto.setCity(name);
//                String[] split = exce.getAddress().split(name);
//                maintainDto.setDistrict(split[1]);
//            } else {
//                //截取字符串 市
//                boolean con = exce.getAddress().contains("市");
//                if (con) {
//                    String[] split = exce.getAddress().split("市");
//                    String substring = split[0].substring(split[0].length() - 2, split[0].length());
//                    System.out.println(substring + "市");
//                    maintainDto.setDistrict(split[1]);
//                    maintainDto.setCity(substring + "市");
//                } else {
//                    maintainDto.setDistrict(exce.getAddress());
//                    maintainDto.setCity(name);
//                }
//            }
            excelInfoList.add(bank);
        });
        bankService.AddBank(excelInfoList);
        if (list.size() > 0) {
            res.put("log", list);
        }
        res.put("success", "导入数据成功条数:" + rowSuccess);
        res.put("error", "导入数据失败条数:" + errorCount);
        long endTime = System.currentTimeMillis();
        String time = String.valueOf((endTime - startTime) / 1000);
        res.put("time", "导入数据用时:" + time + "秒");
//        renderResult(response, res);
        return CommonResult.success(excelInfoList);
    }


    @ApiOperation("银行编码")
    @PostMapping("/addMaintainBankCode")
    public CommonResult addMaintainBankCode(@RequestParam(value = "file", required = false) MultipartFile file) {
        long startTime = System.currentTimeMillis();
        List<String> list = new ArrayList();
        Map<String, Object> res = new HashMap<>();
        int row = 1;
        int rowSuccess = 0;
        Integer errorCount = 0;
        List<BankCode> excelInfo = BankCodePatientExcelUtil.getExcelInfo(file);
        List<BankCode> excelInfoList = new ArrayList<>();
        excelInfo.stream().forEach(exce -> {
            BankCode bank = new BankCode();
            BeanUtils.copyProperties(exce, bank);
            excelInfoList.add(bank);
        });
        bankCodeService.AddBankCode(excelInfoList);
        if (list.size() > 0) {
            res.put("log", list);
        }
        res.put("success", "导入数据成功条数:" + rowSuccess);
        res.put("error", "导入数据失败条数:" + errorCount);
        long endTime = System.currentTimeMillis();
        String time = String.valueOf((endTime - startTime) / 1000);
        res.put("time", "导入数据用时:" + time + "秒");
//        renderResult(response, res);
        return CommonResult.success(excelInfoList);
    }


    /**
     * 导出 e
     *
     * @return
     */
    @ApiOperation("导出")
    @GetMapping("/createExcle")
    public CommonResult<?> createExcle() {
        //第一步创建workbook
        HSSFWorkbook wb = new HSSFWorkbook();

        //第二步创建sheet
        HSSFSheet sheet = wb.createSheet("银行编码");

        //第三步创建行row:添加表头0行
        //4个参数依次为：开始行，结束行，开始列，结束列
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 4));
        HSSFRow row = sheet.createRow(0);

        HSSFCell cell1 = row.createCell(0);
        cell1.setCellValue("编码");
        HSSFCellStyle style = wb.createCellStyle();
//        style.setAlignment(HSSFCellStyle);  //居中

        HSSFRow row1 = sheet.createRow(1);

        //第四步创建单元格
        HSSFCell cell = row1.createCell(0);         //第一个单元格
//        cell.setCellValue("序号id");                  //设定值
//        cell.setCellStyle(style);                   //内容居中

//        cell = row1.createCell(1);                   //第二个单元格
        cell.setCellValue("branch_id");
        cell.setCellStyle(style);

        cell = row1.createCell(1);                   //第三个单元格
        cell.setCellValue("branch_name");
        cell.setCellStyle(style);

        cell = row1.createCell(2);                   //第四个单元格
        cell.setCellValue("bank_no");
        cell.setCellStyle(style);

        cell = row1.createCell(3);                   //第五个单元格
        cell.setCellValue("银行名称");
        cell.setCellStyle(style);


        cell = row1.createCell(4);                   //第6个单元格
        cell.setCellValue("是否支持对公");
        cell.setCellStyle(style);

        //第五步插入数据
        Maintain tbHkhb = new Maintain();
        List<Bank> list = bankService.queryList(1);
        for (int i = 0; i < list.size(); i++) {
            //创建行
            row = sheet.createRow(i + 2);
            //创建单元格并且添加数据
//            row.createCell(0).setCellValue(simpleDateFormat.format(list.get(i).getHbsj()));
//            row.createCell(0).setCellValue(list.get(i).getMaintainId());
            row.createCell(0).setCellValue(list.get(i).getBranchId());
            row.createCell(1).setCellValue(list.get(i).getBranchName());
            row.createCell(2).setCellValue(list.get(i).getBankNo());
            row.createCell(3).setCellValue(list.get(i).getBankName());
            row.createCell(4).setCellValue(list.get(i).getOfThePublic());
        }
        //第六步将生成excel文件保存到指定路径下
        try {
            File file = new File("C:/Users/lenovo/Desktop/表.xls");
            if (!file.exists()) {
                FileOutputStream fout = new FileOutputStream("C:/Users/lenovo/Desktop/yinh记录表.xls");
                wb.write(fout);
                fout.close();
            } else {
                FileOutputStream fout = new FileOutputStream("C:/Users/lenovo/Desktop/yinh记录表s.xls");
                wb.write(fout);
                fout.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return CommonResult.success("ok");
    }


    @RequestMapping(value = "/AddBankTest", method = RequestMethod.GET)
    @ApiOperation("copy数据")
    public CommonResult AddBankTest(@RequestParam("ofThePublic") @ApiParam("是否支持对公 1支持 -1不") Integer ofThePublic) {

        return CommonResult.success(bankService.AddBankTest(ofThePublic));
    }


    @Resource
    private DistrictService districtService;

    @GetMapping("/scriptDemo")
    @ApiOperation("解析js文件")
    public CommonResult scriptDemo() {
        try {
            List<District> script = scriptDemo.Script();
//            script.forEach(s->{
//                District district = new District();
//                BeanUtils.copyProperties(s,district);
//                scr.add(district);
//                district.getChildren().forEach(s1->{
//                    District dis = new District();
//                    BeanUtils.copyProperties(s1,dis);
//                    scr.add(dis);
//                });
//            });
            districtService.AddBankTest(script);
            return CommonResult.success(script);
        } catch (Exception e) {
            return CommonResult.success(e.getMessage());
        }
    }


    @GetMapping("/queryList")
    public CommonResult queryList() {

        return CommonResult.success(districtService.queryList());
    }


    @GetMapping("/queryList11")
    public CommonResult queryList11() {

        return CommonResult.success(districtService.queryList11());
    }

}
